# -*- coding: utf-8 -*-
__author__ = 'zaxlct'
__date__ = '2017/1/10 下午3:48'

from random import Random
from django.core.mail import send_mail
import string
import smtplib
from email.mime.text import MIMEText
from email.header import Header

from aiuser.models import EmailVerifyRecord
from aistudy.settings import EMAIL_FROM, EMAIL_HOST_USER, EMAIL_HOST, EMAIL_HOST_PASSWORD, EMAIL_PORT


def send_ai_email(email, send_type='register', addinfo=None):
    email_record = EmailVerifyRecord()
    code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()


    email_title = ''
    email_content = ''

    if send_type == 'register':
        email_title = '欢迎注册AI社区'
        email_content = '请点击下面的链接激活你的账号：http://www.studyai.com/user/active/{0}'.format(code)
        send_status = send_mail(email_title, email_content, EMAIL_FROM, [email])
        if send_status:
            # TODO 提示发送成功
            return send_status
        else:
            return send_status
    elif send_type == 'findpwd':
        email_title = 'AI社区密码找回'
        email_content = '请点击下面的链接重置你的密码：http://www.studyai.com/user/findpwd/?code={0}'.format(code)
        send_status = send_mail(email_title, email_content, EMAIL_FROM, [email])
        if send_status:
            # TODO 提示发送成功
            return send_status
        else:
            return send_status
    elif send_type == 'savepwd':
        email_title = 'AI社区账号信息'
        email_content = '请点击下面的链接重置你的密码：http://www.studyai.com/user/resetpwd/{0}'.format(code)
        send_status = send_mail(email_title, email_content, EMAIL_FROM, [email])
        if send_status:
            # TODO 提示发送成功
            return send_status
        else:
            return send_status


def send_ai_email_pylib(email, send_type='register', addinfo=None):
    email_record = EmailVerifyRecord()
    code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    email_title = ''
    email_content = ''

    if send_type == 'register':
        email_title = '欢迎注册AI社区！'
        email_content = '请点击下面的链接激活你的账号：http://www.studyai.com/user/active/{0}'.format(code)

        sender = EMAIL_FROM
        receivers = [email]

        message = MIMEText(email_content, 'plain', 'utf-8')
        message['Subject'] = Header(email_title, 'utf-8')
        message['From'] = Header("人工智能学院", 'utf-8')
        message['To'] = Header("账号激活", 'utf-8')

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(EMAIL_HOST, EMAIL_PORT)
            smtpObj.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")

        except smtplib.SMTPException:
            print("Error: 无法发送邮件")


    elif send_type == 'findpwd':
        email_title = 'AI社区密码找回！'
        email_content = '请点击下面的链接重置你的密码：http://www.studyai.com/user/findpwd/?code={0}'.format(code)
        send_status = send_mail(email_title, email_content, EMAIL_FROM, [email])
        if send_status:
            # TODO 提示发送成功
            return send_status
        else:
            return send_status


    elif send_type == 'savepwd':
        email_title = 'AI社区账号信息'
        email_content = '请点击下面的链接重置你的密码：http://www.studyai.com/user/resetpwd/{0}'.format(code)
        send_status = send_mail(email_title, email_content, EMAIL_FROM, [email])
        if send_status:
            # TODO 提示发送成功
            return send_status
        else:
            return send_status







def random_str(randomlength=8):
    str_init = ""
    chars = string.ascii_letters + string.digits  # 26个大小写字母加数字
    length = len(chars) - 1
    random = Random()
    for i in range(randomlength):
        str_init += chars[random.randint(0, length)]
    return str_init
